package com.sdry.product.sorting.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdry.basic.entity.Car;
import com.sdry.basic.entity.Driver;
import com.sdry.common.BaseController;
import com.sdry.common.TableDataInfo;
import com.sdry.product.sorting.entity.SortReceiving;
import com.sdry.product.sorting.entity.SortReceivingDetail;
import com.sdry.product.sorting.service.SortReceivingDetailService;
import com.sdry.product.sorting.service.WarehousingService;
import com.sdry.system.entity.SysUser;
import com.sdry.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: info_sdry
 * @description: 入库管理
 * @author: cb
 * @create: 2020-02-25 16:29
 **/
@Controller
@RequestMapping(value = "warehousing")
public class WarehousingController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private WarehousingService warehousingService;

    @Autowired
    private SortReceivingDetailService sortReceivingDetailService;

    /**
     * 展示页面
     * @return
     */
    @RequestMapping(value = "showWarehousing")
    public String showWarehousing(Model model) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("log_on",0L).eq("del_flag",0L).eq("type",0L);
        List<SysUser> userList = userService.list(queryWrapper);

        //司机
        List<Driver> driverList =  userService.queryDriver();
        //车辆
        List<Car> carList = userService.queryCar();
        model.addAttribute("driverList",driverList);
        model.addAttribute("carList",carList);
        model.addAttribute("userList",userList);
        return "/product/sorting/warehousingList";
    }

    /**
     * 分页查询入库，收货
     * @param sortReceiving 对象
     * @return TableDataInfo
     */
    @RequestMapping(value = "queryWarehousingPage")
    @ResponseBody
    public TableDataInfo queryWarehousingPage(SortReceiving sortReceiving) {
        List<SortReceiving> sortReceivingList = warehousingService.queryWarehousingPage(sortReceiving);
        Long total = warehousingService.queryWarehousingTotal(sortReceiving);
        return getDataTable(sortReceivingList,total);
    }



    /**
     * 添加
     * @param sortReceiving
     * @return
     */
    @RequestMapping(value = "add")
    @ResponseBody
    public boolean add(SortReceiving sortReceiving) {
        return warehousingService.save(sortReceiving);
    }

    /**
     * 删除
     * @param receivingId 主键
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public boolean add(Long receivingId) {
        QueryWrapper<SortReceivingDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiving_id", receivingId);
        List<SortReceivingDetail> list = sortReceivingDetailService.list(queryWrapper);
        if (list.size() > 0) {
            return false;
        }
        SortReceiving sortReceiving = new SortReceiving();
        sortReceiving.setReceivingId(receivingId);
        sortReceiving.setDelFlag("1");
        return warehousingService.updateById(sortReceiving);
    }
}
